package com.dd.ms.storage.web.config.cos;

import com.dd.ms.storage.web.pojo.domain.CosProperties;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.region.Region;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author liuxianmeng
 * @CreateTime 2025/6/23 1:05
 * @Description COS配置
 */
@Configuration
@RequiredArgsConstructor
public class CosConfig {

    private final CosProperties cosProperties;

    @Bean
    public COSClient cosClient() {
        // 1 初始化用户身份信息
        COSCredentials cred = new BasicCOSCredentials(
            cosProperties.getSecretId(),
            cosProperties.getSecretKey()
        );

        // 2 设置存储桶地域
        Region region = new Region(cosProperties.getRegion());
        ClientConfig clientConfig = new ClientConfig(region);

        // 3 创建COS客户端
        return new COSClient(cred, clientConfig);
    }
}
